<?php


namespace App\Engine;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class BaseModel extends Model
{
    protected $dateFormat = 'U';
    protected $table="";
    public $timestamps = true;
    public $with=[];

    public $rule=[];
    public $ruleMessage=[];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function user()
    {
        return $this->belongsTo("App\Models\User","uid","id")->select(["id","nickname","avatar","phone"]);
    }



   public function getCreatedAtAttribute($value)
    {
        return $value;
    }
    public function getUpdatedAtAttribute($value)
    {
        return $value;
    }

    public function setLabelAttribute($value)
    {
        if (!$value)
            $this->attributes["label"]=$value;
        $this->attributes["label"]= json_encode($value,256);
    }

    public function house()
    {
        return $this->belongsTo("App\Models\House","house_id","id")->select("id","name","icon","price_state");
    }

    public function getLabelAttribute($value)
    {
        if (!$value)
            return $value;
        return json_decode($value,256);
    }

    /**数据过滤
     * @param $data
     */
    public function filter_data($data)
    {
        foreach ($data as $key=>$value){
            if(!in_array($key,$this->fillable)&&$key!="id"){
                unset($data[$key]);
            }
        }
        return $data;
    }

    public function setTimeAttribute($value)
    {
        $this->attributes["time"]= strtotime($value);
    }

    public function getTimeAttribute($value)
    {
        return date("Y-m-d H:i:s",$value);
    }

    public function setNicknameAttribute($value)
    {
        $this->attributes["nickname"]=base64_encode($value);
    }

    public function getNicknameAttribute($value)
    {
        return base64_decode($value);
    }

    public function setPositionAttribute($value)
    {
        if ($value){
            $this->attributes["position"]=json_encode($value,256);
        }
        else
            $this->attributes["position"]=json_encode([],256);
    }

    public function getPositionAttribute($value)
    {
        return json_decode($value,256);
    }

}
